package com.yanqu.road.dao.impl.wingroom.chakra;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomChakraBeVisitNum;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomChakraBeVisitRecord;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomChakraDetail;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomFurnitureRewardRecord;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WingRoomFurnitureRewardRecordDaoImpl extends TempDataObjectDao<UserWingRoomFurnitureRewardRecord> {

    private static String insertSql = "INSERT INTO `t_u_wingroom_furniture_reward_record`(`userId`, `furnitureId`, `level`, `createTime`) VALUES (?, ?, ?, ?)";
    @Override
    protected boolean addData(UserWingRoomFurnitureRewardRecord data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFurnitureId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean updateData(UserWingRoomFurnitureRewardRecord data) {
        return false;
    }

    @Override
    protected UserWingRoomFurnitureRewardRecord parseData(ResultSet rs) throws SQLException {
        UserWingRoomFurnitureRewardRecord data = new UserWingRoomFurnitureRewardRecord();
        data.setUserId(rs.getLong("userId"));
        data.setFurnitureId(rs.getInt("furnitureId"));
        data.setLevel(rs.getInt("level"));
        data.setCreateTime(rs.getLong("createTime"));
        return data;
    }

    /**
     * 获取数据
     * @param userId
     * @return
     */
    public Map<Integer, Map<Integer, UserWingRoomFurnitureRewardRecord>> getData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, UserWingRoomFurnitureRewardRecord>> map = new ConcurrentHashMap<>();
        String sql = "select * from t_u_wingroom_furniture_reward_record where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserWingRoomFurnitureRewardRecord data = parse(rs);
                Map<Integer, UserWingRoomFurnitureRewardRecord> levelMap = map.computeIfAbsent(data.getFurnitureId(), k -> new HashMap<>());
                levelMap.put(data.getLevel(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
